package com.zsd.android.apteventbus;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zsd.android.annotation.PermissionCheck;
import com.zsd.android.annotation.Subscribe;
import com.zsd.android.apteventbus.apt.EventBusIndex;
import com.zsd.android.apteventbus.event.UserEvent;
import com.zsd.android.apteventbus.service.ThreadService;
import com.zsd.android.apteventbus.utils.MLog;
import com.zsd.android.apteventbus.utils.PhoneUtil;
import com.zsd.android.eventbus.EventBus;
import com.zsd.android.glidelib.widget.NeBigView;
import com.zsd.android.glidelib.widget.NeBigView2;
import com.zsd.android.okhttplib.Call;
import com.zsd.android.okhttplib.Callback;
import com.zsd.android.okhttplib.OkHttpClient;
import com.zsd.android.okhttplib.Request;
import com.zsd.android.okhttplib.RequestBody;
import com.zsd.android.okhttplib.Response;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;
    private NeBigView bigView;

    private final String[] permissions = {Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECEIVE_BOOT_COMPLETED};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.main_text);
        textView.setOnClickListener(this);
        EventBus.getDefault().addIndex(new EventBusIndex());
        EventBus.getDefault().register(this);

        bigView = findViewById(R.id.main_big_view);
        try {
            InputStream is = getAssets().open("timg.jpg");
            bigView.setImage(is);
        } catch (IOException e) {
            e.printStackTrace();
            MLog.i(e.getMessage());
        }

        
    }

    private boolean checkPermissions() {
        ArrayList<String> needPermissions = new ArrayList<>();
        for (String permission : permissions) {
            int checkSelfPermission = ContextCompat.checkSelfPermission(this, permission);
            if (PackageManager.PERMISSION_DENIED == checkSelfPermission) {
                needPermissions.add(permission);
            }
        }
        if (needPermissions.size() > 0) {
            ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), 200);
            return false;
        }
        return true;
    }

    @PermissionCheck(permissions = {Manifest.permission.CAMERA}, requestCode = 100)
    private void startCamera() {

    }

    @Subscribe()
    public void onEvent(UserEvent event) {
        MLog.i("UserEvent : " + event.toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_text:
                UserEvent event = new UserEvent();
                event.setAge(21);
                event.setName("安静欧舒丹");
                EventBus.getDefault().post(event);
                startService(new Intent(this, ThreadService.class));
                break;
        }
    }

    private void doRequest() {
        OkHttpClient client = new OkHttpClient.Builder().build();
        //POST
        RequestBody requestBody = new RequestBody();
        requestBody.addBody("city", "110101");
        requestBody.addBody("key", "142315fd3415");
        Request request = new Request.Builder()
                .url("")
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailed(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }
}
